package com.eboss.sbs.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.eboss.sbs.R;
import com.eboss.sbs.activity.base.TabBasicActivity;
import com.eboss.sbs.config.Config;
import com.eboss.sbs.enums.EnumConsts;
import com.eboss.sbs.http.API;
import com.eboss.sbs.http.HttpCallback;
import com.eboss.sbs.model.PayBarcodeEntity;
import com.google.gson.reflect.TypeToken;
import com.mytools.tool.utils.LocalParasUtils;
import com.mytools.tool.utils.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ReceiptActivity extends TabBasicActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		mActivity = this;
		setContentView(R.layout.activity_receipt);
		super.onCreate(savedInstanceState);
		initView();
	}
	
	private void initView(){
		showView(R.id.titleback_btn_back, false);
		setTvText(R.id.tv_title, "收款");
		
		findViewById(R.id.layout_barcode_scan).setOnClickListener(this);
		findViewById(R.id.layout_barcode).setOnClickListener(this);
		findViewById(R.id.layout_account_info).setOnClickListener(this);
		findViewById(R.id.layout_receipt_info).setOnClickListener(this);
		findViewById(R.id.layout_rate_info).setOnClickListener(this);
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.layout_barcode_scan:
				pay(1);
				break;
			case R.id.layout_barcode:
				pay(2);
				break;
			case R.id.layout_account_info:
				startActivity(new Intent(mActivity, AccountInfoActivity.class));
				break;
			case R.id.layout_receipt_info:
				startActivity(new Intent(mActivity, ReceiptInfoActivity.class));
				break;
			case R.id.layout_rate_info:
				Intent i = new Intent(mActivity, PicturesShow.class);
				i.putExtra("title", "额度与费率");
				i.putExtra("imgurls", new ArrayList<String>(){{add(Config.URL_RATEINFO);}});
				startActivity(i);
				break;
			default: break;
		}
	}
	
	private void pay(int type){
		String activateCode = LocalParasUtils.getActivateCode(mActivity);
		if(!mApp.getLoginEntity().getCanCheckout()){
			toast("当前不支持终端收款，请联系客服");
		}
		else if(mApp.getLoginEntity().getScanPayType()== EnumConsts.ScanPayType.SQB.getType()&& StringUtil.isNull(activateCode)){
			toast("终端未激活，请联系客服");
		}
		else if(mApp.getLoginEntity().getScanPayType()== EnumConsts.ScanPayType.FUYOU.getType()&&StringUtil.isNull(mApp.getLoginEntity().getFyMerchantNo())){
			toast("商户号未配置，请联系客服");
		}
		else if((mApp.getLoginEntity().getScanPayType()== EnumConsts.ScanPayType.SQB.getType()&&!StringUtil.isNull(activateCode))||
				(mApp.getLoginEntity().getScanPayType()== EnumConsts.ScanPayType.FUYOU.getType()&&!StringUtil.isNull(mApp.getLoginEntity().getFyMerchantNo()))){
			if(type==1){
				startActivity(new Intent(mActivity, PayMoneyInputActivity.class));
			}
			else if(type==2){
				showBarcode();
			}
		}
		else{
			toast("终端未配置成功，请联系客服");
		}
	}
	
	private void showBarcode(){
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("sid", mApp.getLoginEntity().getSid());
		API.http_post(this, API.PAY_BARCORD, params, new HttpCallback<PayBarcodeEntity>(this) {
			@Override
			public void onSuccess(int statusCode, PayBarcodeEntity data) {
				
				String barcode = data.getBarcode();
				if(StringUtil.isNull(barcode)){
					toast("商户未设置收款二维码");
				}
				else{
					Intent intent =  new Intent(mActivity, ReceiptBarcodeActivity.class);
					intent.putExtra("barcode", barcode);
					startActivity(intent);
				}
			}

			@Override
			public void onFailure(int statusCode, String message) {
				super.onFailure(statusCode, message);
			}
			
			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public TypeToken getTypeToken() {
				return new TypeToken<PayBarcodeEntity>() {
				};
			}
		});
		
	}
	
	
	
}
